package com.hskn.hss.module.employeeCorrectRecord.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.module.employeeCorrectRecord.entity.EmployeeCorrectRecord;
import com.hskn.hss.module.employeeCorrectRecord.mapper.EmployeeCorrectRecordMapper;
import com.hskn.hss.module.employeeCorrectRecord.service.EmployeeCorrectRecordService;
import com.hskn.hss.module.employeeCorrectRecord.to.EmployeeCorrectRecordTo;
import com.hskn.hss.module.employeeCorrectRecord.vo.EmployeeCorrectRecordPageVo;
import com.hskn.hss.module.employeePunchCard.entity.EmployeePunchCard;
import com.hskn.hss.module.employeePunchCard.mapper.EmployeePunchCardMapper;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.mapper.GridMapper;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Service
public class EmployeeCorrectRecordServiceImpl extends ServiceImpl<EmployeeCorrectRecordMapper, EmployeeCorrectRecord> implements EmployeeCorrectRecordService {

    @Resource
    private EmployeeCorrectRecordMapper employeeCorrectRecordMapper;
    @Resource
    private EmployeePunchCardMapper employeePunchCardMapper;
    @Resource
    private GridMapper gridMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public EmployeeCorrectRecordPageVo attendanceCorrectRecord(EmployeeCorrectRecordTo employeeCorrectRecordTo) {
        QueryWrapper<EmployeeCorrectRecord> employeeCorrectRecordQueryWrapper = new QueryWrapper<>();
        employeeCorrectRecordQueryWrapper.lambda().between(EmployeeCorrectRecord::getPunchCardDate, employeeCorrectRecordTo.getStartDate(), employeeCorrectRecordTo.getEndDate());
        if (StringUtils.isNotEmpty(employeeCorrectRecordTo.getEmployeeId())) {
            employeeCorrectRecordQueryWrapper.lambda().eq(EmployeeCorrectRecord::getEmployeeId, employeeCorrectRecordTo.getEmployeeId());
        }
        if (StringUtils.isNotEmpty(employeeCorrectRecordTo.getGridId())) {
            employeeCorrectRecordQueryWrapper.lambda().eq(EmployeeCorrectRecord::getGridId, employeeCorrectRecordTo.getGridId());
        }
        if (StringUtils.isNotEmpty(employeeCorrectRecordTo.getDepartmentId())) {
            employeeCorrectRecordQueryWrapper.lambda().eq(EmployeeCorrectRecord::getDepartmentId, employeeCorrectRecordTo.getDepartmentId());
        }
        List<EmployeeCorrectRecord> employeeCorrectRecords = employeeCorrectRecordMapper.selectList(employeeCorrectRecordQueryWrapper);
        List<Grid> gridList = gridMapper.selectList(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridList.stream().collect(Collectors.groupingBy(Grid::getGridId, Collectors.collectingAndThen(toList(), grids -> grids.get(0))));

        for (EmployeeCorrectRecord employeeCorrectRecord : employeeCorrectRecords) {

            employeeCorrectRecord.setBeforeCorrectGotoWorkStatus("正常");
            employeeCorrectRecord.setBeforeCorrectComeWorkStatus("正常");
            employeeCorrectRecord.setAfterCorrectComeWorkStatus("正常");
            employeeCorrectRecord.setAfterCorrectGotoWorkStatus("正常");

            if (employeeCorrectRecord.getLaterIn() == 1) {
                employeeCorrectRecord.setBeforeCorrectGotoWorkStatus("迟到");
            }
            if (employeeCorrectRecord.getLeaveEarly() == 1) {
                employeeCorrectRecord.setBeforeCorrectComeWorkStatus("早退");
            }
            if (null == employeeCorrectRecord.getBeforeCorrectGotoWork() || "".equals(employeeCorrectRecord.getBeforeCorrectGotoWork())) {
                employeeCorrectRecord.setBeforeCorrectGotoWorkStatus("缺卡");
            }
            if ((null == employeeCorrectRecord.getBeforeCorrectGotoWork() || "".equals(employeeCorrectRecord.getBeforeCorrectGotoWork())) && (null == employeeCorrectRecord.getBeforeCorrectComeWork() || "".equals(employeeCorrectRecord.getBeforeCorrectComeWork()))) {
                employeeCorrectRecord.setBeforeCorrectGotoWorkStatus("旷工");
            }

            Grid grid = gridMap.get(employeeCorrectRecord.getGridId());
            if (null != grid) {
                employeeCorrectRecord.setGridName(grid.getName());
                if (DateUtil.parse(grid.getStartTime()).getTime() > DateUtil.parse(employeeCorrectRecord.getAfterCorrectGotoWork()).getTime()) {
                    employeeCorrectRecord.setAfterCorrectGotoWorkStatus("迟到");
                }
                if (DateUtil.parse(grid.getEndTime()).getTime() < DateUtil.parse(employeeCorrectRecord.getAfterCorrectComeWork()).getTime()) {
                    employeeCorrectRecord.setAfterCorrectGotoWorkStatus("早退");
                }
            }
            if (null == employeeCorrectRecord.getAfterCorrectGotoWork() || "".equals(employeeCorrectRecord.getAfterCorrectGotoWork())) {
                employeeCorrectRecord.setAfterCorrectGotoWorkStatus("缺卡");
            }
            if ((null == employeeCorrectRecord.getAfterCorrectGotoWork() || "".equals(employeeCorrectRecord.getAfterCorrectGotoWork())) && (null == employeeCorrectRecord.getAfterCorrectComeWork() || "".equals(employeeCorrectRecord.getAfterCorrectComeWork()))) {
                employeeCorrectRecord.setAfterCorrectGotoWorkStatus("旷工");
            }
        }
        return page(employeeCorrectRecordTo, employeeCorrectRecords);
    }

    private EmployeeCorrectRecordPageVo page(EmployeeCorrectRecordTo employeeCorrectRecordTo, List list) {
        // 分页
        Long page = StringUtils.isEmpty(employeeCorrectRecordTo.getPage()) ? 1 : employeeCorrectRecordTo.getPage();
        Long size = StringUtils.isEmpty(employeeCorrectRecordTo.getSize()) ? 10 : employeeCorrectRecordTo.getSize();
        EmployeeCorrectRecordPageVo employeeCorrectRecordPageVo = new EmployeeCorrectRecordPageVo();

        employeeCorrectRecordPageVo.setTotal((long) list.size());
        employeeCorrectRecordPageVo.setCurrent(page);
        employeeCorrectRecordPageVo.setSize(size);
        BigDecimal pageSizeB = new BigDecimal(size);
        BigDecimal sizeB = new BigDecimal(list.size());
        employeeCorrectRecordPageVo.setPages(Long.valueOf(sizeB.divide(pageSizeB, BigDecimal.ROUND_CEILING).toString()));

        employeeCorrectRecordPageVo.setList(new ArrayList<>());
        for (int i = 0; i < list.size(); i++) {
            if (i >= size * (page - 1) && i < size * page) {
                employeeCorrectRecordPageVo.getList().add(list.get(i));
            }
        }
        return employeeCorrectRecordPageVo;
    }

    @Override
    @Transactional
    public void insertEmployeeCorrectRecord(EmployeeCorrectRecordTo employeeCorrectRecordTo) {
        EmployeeCorrectRecord employeeCorrectRecord = new EmployeeCorrectRecord();
        EmployeePunchCard employeePunchCard = employeePunchCardMapper.selectById(employeeCorrectRecordTo.getEmployeePunchCardId());
        employeeCorrectRecord.setEmployeeId(employeePunchCard.getEmployeeId());
        employeeCorrectRecord.setEmployeeName(employeePunchCard.getEmployeeName());
        employeeCorrectRecord.setDepartmentId(employeePunchCard.getDepartmentId());
        employeeCorrectRecord.setDepartmentName(employeePunchCard.getDepartmentName());
        employeeCorrectRecord.setPunchCardDate(employeePunchCard.getPunchCardDate());
        employeeCorrectRecord.setBeforeCorrectGotoWork(employeePunchCard.getWorkTime());
        employeeCorrectRecord.setBeforeCorrectComeWork(employeePunchCard.getAfterWorkTime());
        employeeCorrectRecord.setAfterCorrectGotoWork(employeeCorrectRecordTo.getAfterCorrectGotoWork());
        employeeCorrectRecord.setAfterCorrectComeWork(employeeCorrectRecordTo.getAfterCorrectComeWork());
        User user = LoginUtils.getUser();
        employeeCorrectRecord.setCorrectPersonId(user.getUid());
        User user1 = userMapper.selectById(user.getUid());
        employeeCorrectRecord.setCorrectPerson(user1.getName());
        employeeCorrectRecord.setCorrectReason(employeeCorrectRecordTo.getCorrectReason());
        employeeCorrectRecord.setCorrectTime(new Date());
        employeeCorrectRecord.setEmployeePunchCardId(employeePunchCard.getId());
        employeeCorrectRecord.setGridId(employeePunchCard.getGridId());
        employeeCorrectRecord.setStatus(0);
        employeeCorrectRecord.setLackCard(employeePunchCard.getLackCard());
        employeeCorrectRecord.setLaterIn(employeePunchCard.getLaterIn());
        employeeCorrectRecord.setLeaveEarly(employeePunchCard.getLeaveEarly());
        employeeCorrectRecord.setNoWork(employeePunchCard.getNoWork());
        employeeCorrectRecordMapper.insert(employeeCorrectRecord);
        employeePunchCard.setActualWorkTime(employeeCorrectRecordTo.getAfterCorrectGotoWork() + ":00");
        employeePunchCard.setActualAfterWorkTime(employeeCorrectRecordTo.getAfterCorrectComeWork() + ":00");
        employeePunchCardMapper.updateById(employeePunchCard);
    }

    @Override
    @Transactional
    public EmployeeCorrectRecord updateEmployeeCorrectRecord(EmployeeCorrectRecordTo employeeCorrectRecordTo) {
        EmployeeCorrectRecord employeeCorrectRecord = employeeCorrectRecordMapper.selectById(employeeCorrectRecordTo.getEmployeeCorrectRecordId());
        if (employeeCorrectRecord.getStatus() == 0) {
            EmployeePunchCard employeePunchCard = employeePunchCardMapper.selectById(employeeCorrectRecord.getEmployeePunchCardId());
            employeePunchCard.setActualWorkTime(employeeCorrectRecord.getBeforeCorrectGotoWork() + ":00");
            employeePunchCard.setActualAfterWorkTime(employeeCorrectRecord.getBeforeCorrectComeWork() + ":00");
            employeePunchCardMapper.updateById(employeePunchCard);

            employeeCorrectRecord.setStatus(1);
            employeeCorrectRecordMapper.updateById(employeeCorrectRecord);
        }
        return employeeCorrectRecord;
    }
}
